package org.benxiong.iplay.util;

/**
 * Path
 * 
 * @author ben xiong
 * 
 */
public class PathUtil {

	public static String getUserPath(String username) {
		if (username == null)
			return "";

		username = username.trim();

		if (username.length() < 3) {
			return "/" + username;
		}

		StringBuffer sb = new StringBuffer("/");
		sb.append(username.substring(0, 1)).append("/")
				.append(username.subSequence(1, 2)).append("/")
				.append(username.subSequence(2, 3)).append("/")
				.append(username);
		return sb.toString();
	}

	/**
	 * get url of profile picture
	 * 
	 * @param username
	 * @param version
	 * @return
	 */
	public static String profilePictureUrl(String username, Long version) {
		if (GlobalVar.environment.equals("dev"))
			return PathUtil.testThumbnailUrl();
		String url = GlobalVar.photoRootUrl + PathUtil.getUserPath(username) + "/home.jpg";
		if (version != null) {
			return url + "?v=" + version;
		}
		return url;
	}

	public static String coverPictureUrl(String username, Long version) {
		String url = GlobalVar.photoRootUrl + PathUtil.getUserPath(username) + "/cover.jpg";
		if (version != null) {
			return url + "?v=" + version;
		}
		return url;
	}

	/**
	 * get url of big profile picture
	 * 
	 * @param username
	 * @param version
	 * @return
	 */
	public static String profilePictureOfStandardSizeUrl(String username, Long version) {
		String url = GlobalVar.photoRootUrl + PathUtil.getUserPath(username) + "/avatar.jpg";
		if (version != null) {
			return url + "?v=" + version;
		}
		return url;
	}

	/**
	 * get url of album photo thumbnail
	 * 
	 * @param username
	 * @param seed
	 * @return
	 */
	public static String userPhotoThumbnailUrl(String username, String seed) {
		String base = GlobalVar.photoRootUrl + PathUtil.getUserPath(username);
		return base + "/" + seed + ".jpg";
	}

	/**
	 * get url of album photo
	 * 
	 * @param username
	 * @param seed
	 * @param secretKey
	 * @return
	 */
	public static String userPhotoStandardUrl(String username, String seed, String secretKey) {
		String base = GlobalVar.photoRootUrl + PathUtil.getUserPath(username);
		return base + "/" + seed + ":" + secretKey.substring(0, 5) + ".jpg";
	}

	public static String messagePhotoThumbnailUrl(String identifier) {
		if (identifier != null && identifier.startsWith("/")) {
			identifier = identifier.substring(1);
		}
		return GlobalVar.photoRootUrl + "/photo.chat/photos/" + identifier + ".jpg";
	}

	public static String messagePhotoFullUrl(String identifier) {
		if (identifier != null && identifier.startsWith("/")) {
			identifier = identifier.substring(1);
		}
		return GlobalVar.photoRootUrl + "/photo.chat/photos/" + identifier + ":original.jpg";
	}

	public static String messageVoiceUrl(String identifier) {
		return GlobalVar.photoRootUrl + "/photo.chat/voices" + identifier + ".aac";
	}

	public static String testThumbnailUrl() {
		String prefix = "http://s3-us-west-2.amazonaws.com/gdt.data.backup/thumbnails/";
		int index = Utils.randomIndex(387);
		if (index < 10) {
			return prefix + "thumb-00" + index + ".jpeg";
		} else if (index < 100) {
			return prefix + "thumb-0" + index + ".jpeg";
		}
		return prefix + "thumb-" + index + ".jpeg";
	}
}
